<?php
namespace app\home\model;
use think\Model;

class MemberModel extends Model
{
	protected $table  = 'qm_member';



	/**
	*
	*open-id查找某个用户
	**/
	public function getMemberIdWhereOpenId($open_id, $field = '*')
	{
		$info = $this->where('open_id', '=', $open_id)->field($field)->find();
		if(!empty($info))
		{
			$info = $info->toArray();
		}

		return $info;
	}



	/**
	*
	*unionid确定用户是否注册
	**/
	public function getMemberIdWhereUnionid($unionid, $field = '*')
	{
		$info = $this->where('unionid', '=', $unionid)->field($field)->find();
		return $info;
	}


	/**
	*
	*member-id查找某个用户
	**/
	public function getMemberIdWhereMemberId($member_id, $field = '*')
	{
		$info = $this->where('member_id', '=', $member_id)->field($field)->find();
		return $info;
	}

	/**
	*
	*添加一个用户
	**/
	public function addMemberInfo($data)
	{
		$res = $this->insertGetId($data);
		return $res;
	}



	/**
	*
	*更新用户数据
	**/
	public function updateMemberInfo($member_id, $update)
	{
		$res = $this->where('member_id', '=', $member_id)->update($update);
		return $res;
	}




	/**
	*
	*根据条件查找用户数据
	**/
	public function geetMemberWhereReturnInfo($where, $field = '*')
	{
		$info = $this->where($where)->field($field)->find();
		return $info;
	}



	/**
	*
	*直邀顾问-总数
	**/
	public function geetMemberInviteGradeTwoCount($invite, $grade = 2)
	{
		$number = $this->where('parent', '=', $invite)->where('grade', '=', $grade)->count('member_id');
		return $number;
	}


	/**
	*
	*团队顾问-人数
	**/
	public function geetMemberTeamNumber($invite)
	{
		$list = $this->where('parent', '=', $invite)->field('member_id, grade, invite')->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}

		return $list;
	}


	/**
	*
	*团队-人数
	**/
	public function getMemberTeamOrderNumber($invite)
	{
		$list = $this->where('parent', '=', $invite)->field('member_id, invite')->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}

		return $list;
	}



	/**
	*
	*我的一，二级总人数
	*
	**/
	public function getMyInviteCounts($invite)
	{
		$list = $this->where('parent', '=', $invite)->column('invite');

		//循环查找二级的用户
		$twolist = [];
		if(!empty($list))
		{
			$twolist = $this->where('parent', 'in', $list)->column('invite');
		}
		

		$counts = count($list) + count($twolist);
		return $counts;
	}



	/**
	*
	*直属下级 + 已买课列表
	**/
	public function getMemberSubordinate($invite, $page = 1)
	{
		//取出所有直属下级
		$limit = 10;
		$keys = ($page - 1) * $limit;
		$one_list = $this->alias('m')->where('m.parent', '=', $invite)->where('o.status', '=', 4)
					->join('qm_order o', 'm.member_id = o.member_id')->order('m.add_time')->limit($keys, $limit)
					->field('m.member_id, m.logo, m.grade, m.nickname, m.invite,m.add_time')->group('o.member_id')->select();
		return $one_list;
	}


	/**
	*
	*直属下级
	*
	**/
	public function getMemberZhishuXiaji($invite, $page = 1)
	{
		$limit = 10;
		$keys = ($page - 1) * $limit;
		$one_list = $this->where('parent', '=', $invite)->order('add_time DESC')->limit($keys, $limit)
						->field('member_id, logo, grade, nickname, invite,add_time')->group('member_id')->select();
		return $one_list;

	}



	/**
	*
	*下下级 + 已买课列表
	**/
	public function getMemberTwoSubordinate($invite, $page = 1)
	{
		//取出所有直属下级的邀请码集合
		$list = $this->where('parent', '=', $invite)->column('invite');
		$twolist = [];

		//找出下下级
		if(!empty($list))
		{
			$limit = 10;
			$keys = ($page - 1) * $limit;
			$twolist = $this->alias('m')->where('m.parent', 'in', $list)->where('o.status', '=', 4)
					->join('qm_order o', 'm.member_id = o.member_id')->order('m.add_time')->limit($keys, $limit)
					->field('m.member_id, m.logo, m.grade, m.nickname, m.invite,m.add_time')->group('o.member_id')->select();
		}
		return $twolist;
	}



	/**
	*
	*下下级
	*
	**/
	public function getMemberZhishuXiaXiaji($invite, $page = 1)
	{	
		$list = $this->where('parent', '=', $invite)->column('invite');
		$twolist = [];

		if(!empty($list))
		{
			$limit = 10;
			$keys = ($page - 1) * $limit;
			$twolist = $this->where('parent', 'in', $list)->order('add_time DESC')->limit($keys, $limit)
							->field('member_id, logo, grade, nickname, invite,add_time')->group('member_id')->select();
		}
		
		return $twolist;

	}




	/**
	*
	*
	*我的团队-总人数
	**/
	public function getMyTeamMiddleCount($member_id, $invite)
	{
		//直属下级+已买课总人数
		$one_count = $this->where('parent', '=', $invite)->count('*');


		//下下级购课总人数
		$list = $this->where('parent', '=', $invite)->column('invite');
		$two_count = 0;

		//找出下下级
		if(!empty($list))
		{
			$two_count = $this->where('parent', 'in', $list)->count('*');
		}

		//访客总人数
		$access_id_list = $this->table('qm_member_access')->where('member_id', '=', $member_id)->column('access_id');
		$three_count = 0;
		if(!empty($access_id_list))
		{
			$three_count = $this->where('member_id', 'in', $access_id_list)->where('parent', '=', null)->count('*');
		}
		
		return [$one_count, $two_count, $three_count];
	}




	/**
	*
	*一级+二级
	*本月邀请的总人数  
	*
	**/
	public function monthInviteCounts($invite)
	{
		$atart = date('Y-m-01',time());
		$end = date('Y-m-t 23:59:59',time());
		$where = [
			['add_time','>=', $atart],
			['add_time', '<=', $end]
		];

		$list = $this->where('parent', '=', $invite)->where($where)->column('invite');

		//循环查找二级的用户
		$twolist = [];
		if(!empty($list))
		{
			$twolist = $this->where('parent', 'in', $list)->where($where)->column('invite');
		}
		
		$counts = count($list) + count($twolist);
		return $counts;
	}


	/**
	*
	*查找用户的上级
	*
	**/
	public function getMemberParentInfosAward($invite, $field = '*')
	{
		$info = $this->where('invite', '=', $invite)->field($field)->find();
		return $info;
	}



	/**
	*
	*找出已绑定邀请人的用户
	*
	**/
	public function getMemberWhereReturnList($field = '*')
	{
		$where = [
			['grade', '=', 1],
		];
		$list = $this->where($where)->where('parent', 'exp', 'is not null')->field($field)->select();
		
		if(!empty($list))
		{
			$list = $list->toArray();
		}
		return $list;
	}



	/**
	*
	*找出所有顾问or星级顾问
	*
	**/
	public function getMemberWhereUpgradeReturnList($grade = 2, $field = '*')
	{
		$where = [
			['grade', '=', $grade],
		];
		$list = $this->where($where)->field($field)->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}
		return $list;
	}


}
